<template>
  <el-dialog title="产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label=" 产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入 产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="accessoryTypeCode">
        <el-select v-model="queryParams.accessoryTypeCode" placeholder="请选择分类" clearable>
          <el-option
            v-for="dict in dict.type.product_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enableFlag">
        <el-select v-model="queryParams.enableFlag" placeholder="请选择是否启用" clearable>
          <el-option
            v-for="dict in dict.type.sys_no_yes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体积长度" prop="volumeLong">
        <el-input
          v-model="queryParams.volumeLong"
          placeholder="请输入体积长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体积宽度" prop="volumeWidth">
        <el-input
          v-model="queryParams.volumeWidth"
          placeholder="请输入体积宽度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体积高度" prop="volumeHight">
        <el-input
          v-model="queryParams.volumeHight"
          placeholder="请输入体积高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品颜色" prop="colorCode">
        <el-select v-model="queryParams.colorCode" placeholder="请选择产品颜色" clearable>
          <el-option
            v-for="dict in dict.type.color"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="productList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedClientId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label=" 产品名称" align="center" prop="productName" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.unit_measure" :value="scope.row.unitOfMeasure"/>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="accessoryTypeCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_type" :value="scope.row.accessoryTypeCode"/>
        </template>
      </el-table-column>
      <el-table-column label="总库存" align="center" prop="totalInventory" />
      <el-table-column label="当前通用库存" align="center" prop="commonCurInventory" />
      <el-table-column label="当前定制库存" align="center" prop="customCurInventory" />
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_no_yes" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="体积长度" align="center" prop="volumeLong" />
      <el-table-column label="体积宽度" align="center" prop="volumeWidth" />
      <el-table-column label="体积高度" align="center" prop="volumeHight" />
      <el-table-column label="产品颜色" align="center" prop="colorCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.color" :value="scope.row.colorCode"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/oa/md/product";

export default {
  name: "ProductSingle",
  dicts: ['product_type', 'color', 'unit_measure', 'sys_no_yes'],
  data() {
    return {
      showFlag:false,
      // 遮罩层
      loading: true,
      selectedClientId: undefined,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,        productCode: null,        accessoryTypeCode: null,        enableFlag: null,        createBy: null,        updateBy: null,        remark: null,        volumeLong: null,        volumeWidth: null,        volumeHight: null,        colorCode: null      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: " 产品名称不能为空", trigger: "blur" }
        ],
        // productCode: [
        //   { required: true, message: "产品编码不能为空", trigger: "blur" }
        // ],
        unitOfMeasure: [
          { required: true, message: "单位不能为空", trigger: "change" }
        ],        accessoryTypeCode: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],      },
      isAdding: false // 控制是否为添加操作
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        productName: null,        productCode: null,        unitOfMeasure: null,        accessoryTypeCode: null,        totalInventory: null,        commonCurInventory: null,        customCurInventory: null,        enableFlag: null,        deleted: null,        createTime: null,        createBy: null,        updateTime: null,        updateBy: null,        remark: null,        volumeLong: null,        volumeWidth: null,        volumeHight: null,        colorCode: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleCurrent(row){
      if(row){
        this.selectedRow = row;
      }
    },
    handleRowChange(row) {
      debugger;
      if(row){
        this.selectedRow = row;
      }
    },
    //行双击选中
    handleRowDbClick(row){
      if(row){
        this.selectedRow = row;
        this.$emit('onSelected',this.selectedRow);
        this.showFlag = false;
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  confirmSelect(){
    if(this.selectedClientId ==null || this.selectedClientId==0){
      this.$notify({
        title:'提示',
        type:'warning',
        message: '请至少选择一条数据!'
      });
      return;
    }
    this.$emit('onSelected',this.selectedRow);
    this.showFlag = false;
  }
  }
};
</script>
